﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace StoreApp
{
    public class Store : IStore
    {
        private readonly List<Item> m_itemCollection;
        private IPaySystem m_paySystem;

        public Store(IPaySystem paySystem)
        {
            m_itemCollection = new List<Item>();
            m_paySystem = paySystem;
        }

        public void Insert(Item item)
        {
            if (item == null)
                throw new NullReferenceException("book");
            m_itemCollection.Add(item);
        }

        public int Count()
        {
            return m_itemCollection.Count;
        }

        public List<Item> Items
        {
            get { return m_itemCollection; }
        }

        public IPaySystem PaySystem
        {
            get { return m_paySystem; }
        }

        public void Remove(Item book)
        {
            m_itemCollection.Remove(book);
        }

        public List<string> Select(Func<Item, string> func)
        {
            return m_itemCollection.Select(func).ToList();
        }

        public Item Find(Predicate<Item> predicate)
        {
            return m_itemCollection.Find(predicate);
        }

        public List<Item> Where(Func<Item, bool> func)
        {
            return m_itemCollection.Where(func).ToList();
        }

        public void Sale(Item item)
        {
            
            if (item == null) throw new ArgumentNullException("item");
            
            if(item.Price < 0) throw new ArgumentOutOfRangeException("item < 0");

            if (m_itemCollection.Remove(item))
            {
                PaySystem.IncreaseProfit(item.Price);
            }
            else
            {
                throw new Exception("Item is not find");
            }
        }
    }
}
